﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using MayhemCore;
using System.ComponentModel;
using System.Threading;
using MayhemWpf.ModuleTypes;
using MayhemWpf.UserControls;
using System.Management;
using Microsoft.Win32;
using System.Security.Principal;
using ComputerMayhem.Wpf;

namespace ComputerMayhem.Events
{
    [DataContract]
    [MayhemModule("Unread Email Count", "Triggers when your unread email count exceeds a number")]
    public class UnreadEmailCount : EventBase, IWpfConfigurable
    {
        [DataMember]
        private string emailToCheck;

        [DataMember]
        private int numberOfUnreadEmail;

        protected override void OnLoadDefaults()
        {
            numberOfUnreadEmail = 10;
        }

        private ManagementEventWatcher watcher;

        private void HandleEvent(object sender, EventArrivedEventArgs e)
        {
            var currentUser = WindowsIdentity.GetCurrent();
            int CurrentUnreadEmailsCount = (int)Registry.GetValue(@"HKEY_USERS\" + currentUser.User.Value + @"\SOFTWARE\Microsoft\Windows\CurrentVersion\UnreadMail\" + emailToCheck, "MessageCount", null);
            if (CurrentUnreadEmailsCount >= numberOfUnreadEmail)
            {
                Trigger();
            }
        }

        protected override void OnEnabling(EnablingEventArgs e)
        {
            try
            {
                var currentUser = WindowsIdentity.GetCurrent();
                WqlEventQuery query = new WqlEventQuery(
                         "SELECT * FROM RegistryValueChangeEvent WHERE " +
                         "Hive = 'HKEY_USERS'" +
                         " AND KeyPath = '" + currentUser.User.Value + @"\\Software\\Microsoft\\Windows\\CurrentVersion\\UnreadMail\\" + emailToCheck +"' AND ValueName='MessageCount'");

                watcher = new ManagementEventWatcher(query);
            }
            catch (Exception)
            {
                ;
            }

            if (watcher != null)
            {
                watcher.EventArrived += new EventArrivedEventHandler(HandleEvent);
                watcher.Start();
            }
        }

        protected override void OnDisabled(DisabledEventArgs e)
        {
            if (watcher != null)
            {
                watcher.Stop();
                watcher.EventArrived -= new EventArrivedEventHandler(HandleEvent);
            }
        }

        public WpfConfiguration ConfigurationControl
        {
            get
            {
                return new UnreadEmailCountConfig(emailToCheck, numberOfUnreadEmail);
            }
        }

        public void OnSaved(WpfConfiguration configurationControl)
        {
            UnreadEmailCountConfig config = configurationControl as UnreadEmailCountConfig;
            numberOfUnreadEmail = config.NumberOfUnreadEmails;
            emailToCheck = config.EmailToCheck;

        }

        public string GetConfigString()
        {
            return "Greater than or equal " + numberOfUnreadEmail.ToString() + " for " + emailToCheck + ".";
        }


    }
}
